# -*- coding:utf-8 -*-
'''eof
name:申请人当前逾期校验
code:Ind_Default_CurrentCheck
tableName:
columnName:
groups:准入>个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

'''
贷款明细信息 ICRLoanInfo002*Loan
当前逾期期数 CurRoverDueCYC

贷记卡明细信息 ICRLoancardInfo002*Card
当前逾期期数 CurRoverDueCYC

准贷记卡明细信息  ICRStdLoancardInfo002*Std

24个月还款状态 Latest24State

指标值=
1.计算贷款逾期数：
在《贷款信息》表中统计当前逾期期数不为0或空的条数；
select count * from 贷款信息 where 当前逾期期数≠ （0 or null） as 贷款逾期数；
2.在《贷记卡信息》表中统计当前逾期期数不为0或空的条数；
select count * from 贷记卡信息 where 当前逾期期数 ≠ （0 or null）as 贷记卡逾期数；
3.在《准贷记卡信息》表中统计“24个月还款状态”字段中从左至右最后一位字符为数字的条数，如果该字符不是数字则返回0；
select count * from 准贷记卡信息 as 准贷记卡逾期数 where 24个月还款状态中从左至右最后一位字符（Latest24State）为数字 ，if 该字符不是数字则返回0;
4.Factor = 贷款逾期期数 + 贷记卡逾期数 + 准贷记卡逾期数"

'''


import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')



def Ind_Default_CurrentCheck():

     try:
        null_type_list = ['', None, 'null', '/', 'Null', 'NULL', '--', '-']
        non_type = [u'N', u'*', u'1', u'2']
        cur_type = [u'3', u'4', u'5', u'6', u'7']
        count_loan = []
        count_card = []
        count_std = []
        if IC in null_type_list:
            return u'缺失值'
        if len(IC) == 0:
            return u'缺失值'
        icr = json.loads(IC[0]["icr"])
        if icr in null_type_list:
            return u'缺失值'
        if len(icr) == 0:
            return u'缺失值'
        #贷款信息
        if icr.has_key('ICRLoanInfo002*Loan') and len(icr['ICRLoanInfo002*Loan']) >= 1:
            for i in icr['ICRLoanInfo002*Loan']:
                if i.has_key('CurRoverDueCYC'):
                    #逾期期数应该为整数
                    try:
                        if int(float(i['CurRoverDueCYC'])) == float(i['CurRoverDueCYC']):
                            if float(i['CurRoverDueCYC']) > 0:
                                count_loan.append(1)
                            else:
                                count_loan.append(0)
                        else:
                            count_loan.append(None)
                    except:
                        count_loan.append(None)
                        continue
                else:
                      count_loan.append(None)
        #若count均为None,则返回缺失值,否则将None置为0
        count_loan1 = filter(lambda x: x is not None, count_loan)
        count_overloan=sum(count_loan1) if len(count_loan1) != 0 else None

        # 贷记卡信息
        if icr.has_key('ICRLoancardInfo002*Card') and len(icr['ICRLoancardInfo002*Card']) >= 1:
            for i in icr['ICRLoancardInfo002*Card']:
                if i.has_key('CurRoverDueCYC'):
                    try:
                        #当前逾期期数应该为整数
                        if int(float(i['CurRoverDueCYC'])) == float(i['CurRoverDueCYC']):
                            if float(i['CurRoverDueCYC']) > 0:
                                count_card.append(1)
                            else:
                                count_card.append(0)
                        else:
                            count_card.append(None)
                    except:
                        count_card.append(None)
                        continue
                else:
                    count_card.append(None)
        # 若count均为None,则返回缺失值,否则将None置为0
        count_card1 = filter(lambda x: x is not None, count_card)
        count_overcard = sum(count_card1) if len(count_card1) != 0 else None


        #准贷记卡信息
        if icr.has_key('ICRStdLoancardInfo002*Std') and len(icr['ICRStdLoancardInfo002*Std']) >= 1:
            for i in icr['ICRStdLoancardInfo002*Std']:
                if i.has_key('Latest24State') :
                    try:
                        if i['Latest24State'][-1] in cur_type:
                            count_std.append(1)
                        elif i['Latest24State'][-1] in non_type:
                            count_std.append(0)
                        else:
                            count_std.append(None)
                    except:
                        count_std.append(None)
                        continue
                else:
                    count_std.append(None)
        # 若count均为None,则返回缺失值,否则将None置为0
        count_std1 = filter(lambda x: x is not None, count_std)
        count_overstd = sum(count_std1) if len(count_std1) != 0 else None
        # 指标值取三个值的和,若均为空则返回缺失值
        total_overnum = [count_overloan, count_overcard, count_overstd]
        overnum = filter(lambda x: x is not None, total_overnum)
        return sum(overnum) if len(overnum) != 0 else u'缺失值'
     except:
        return u'缺失值'

result = Ind_Default_CurrentCheck()